const AuthUtil = require('../utils/AuthUtil');
const { OrderType, CoinType } = require('../constants/DbEnum');
const ErrorProxy = require('../exception/BusinessExceptionProxy');

/**
 * CreateOrderRequest
 */
class CreateOrderRequest {
  /**
   * constructor
   * @param {Object} event
   */
  constructor(event) {
    // take params
    const username = AuthUtil.getUsername(event);

    const {
      orderType,
      baseCoinType,
      baseCoinAddress,
      baseCoinAmount,
      counterCoinType,
      price,
    } = event.body;

    // validate params
    const invalidParams = new Array();

    if (!OrderType[orderType]) {
      invalidParams.push('orderType');
    }
    if (!CoinType[baseCoinType]) {
      invalidParams.push('baseCoinType');
    }
    if (!CoinType[counterCoinType]) {
      invalidParams.push('counterCoinType');
    }
    if (baseCoinAmount < 0.01) {
      invalidParams.push('baseCoinAmount');
    }
    if (price < 0.01) {
      invalidParams.push('price');
    }

    if (invalidParams.length > 0) {
      ErrorProxy.parameterValueError(invalidParams);
    }

    // build record
    this.username = username;
    this.orderType = orderType;
    this.baseCoinType = baseCoinType;
    this.baseCoinAddress = baseCoinAddress;
    this.baseCoinAmount = baseCoinAmount;
    this.counterCoinType = counterCoinType;
    this.price = price;
    this.counterCoinAmount = price * baseCoinAmount;
    this.currentBaseCoinAmount = baseCoinAmount;
  }
}

module.exports = CreateOrderRequest;
